package com.sf.fy.uav.applet.controller.operate;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.sf.fy.uav.common.request.operate.SysPromotionStatisticsRequest;
import com.sf.fy.uav.common.response.operational.SysPromotionStatisticsResponse;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.common.vo.TotalAmountVo;
import com.sf.fy.uav.rest.PromotionStatisticsRest;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author LTQ
 * @since 2023-08-16
 */
@RestController
@RequestMapping("/home/promotion-statistics")
@Tag(name = "推广用户统计管理", description = "推广用户统计管理")
public class SysPromotionStatisticsController {
    private final static Logger logger = LoggerFactory.getLogger(SysPromotionStatisticsController.class);
    @Autowired
    private PromotionStatisticsRest promotionStatisticsRest;


    @GetMapping("get-promotion-statistics")
    @Operation(tags = "获取推广用户统计", description = "获取推广用户统计")
    @CrossOrigin
    @Parameter(name = "page", description = "页码")
    @Parameter(name = "size", description = "单页条数")
    @Parameter(name = "phoneNumber", description = "手机号码")
    @Parameter(name = "activityId", description = "活动id")
    public Rs<TotalAmountVo> getPromotionStatistics(@RequestParam Integer page, @RequestParam Integer size, @RequestParam(required = false) String phoneNumber
            , @RequestParam(required = false) String activityId) {

        SysPromotionStatisticsRequest sysPromotionStatisticsRequest = new SysPromotionStatisticsRequest();
        sysPromotionStatisticsRequest.setPage(page);
        sysPromotionStatisticsRequest.setSize(size);
        sysPromotionStatisticsRequest.setPhoneNumber(phoneNumber);
        sysPromotionStatisticsRequest.setActivityId(activityId);
        return Rs.success(promotionStatisticsRest.getPromotionStatistics(sysPromotionStatisticsRequest));
    }
    @GetMapping("export")
    @ApiOperation(value = "导出", notes = "/export")
    public void export(HttpServletResponse response, @RequestParam Integer page, @RequestParam Integer size,@RequestParam(required = false) String phoneNumber, @RequestParam(required = false) String activityId) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=推广用户表-" + sdf.format(new Date()) + ".xls");
            ExportParams exportParams = new ExportParams();
            Rs<TotalAmountVo> promotionStatistics = this.getPromotionStatistics(page, size, phoneNumber, activityId);
            DateFormat fat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SysPromotionStatisticsResponse  sysPromotionStatisticsResponse: promotionStatistics.getData().getSysPromotionStatisticsResponseList()) {
                if(sysPromotionStatisticsResponse.getStartActivityTime()!=null){
                    sysPromotionStatisticsResponse.setStartPromoteTime(fat.format(sysPromotionStatisticsResponse.getStartActivityTime()));
                }
                if(sysPromotionStatisticsResponse.getTotalReward()!=null){
                    sysPromotionStatisticsResponse.setTotalReward(sysPromotionStatisticsResponse.getTotalReward().setScale(2, RoundingMode.HALF_UP));
                }
            }
            List<SysPromotionStatisticsResponse> sysPromotionStatisticsResponseList = promotionStatistics.getData().getSysPromotionStatisticsResponseList();
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, SysPromotionStatisticsResponse.class, sysPromotionStatisticsResponseList);
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @GetMapping("get-total")
    @CrossOrigin
    @Operation(tags = "获取推广用户统计条数", description = "获取推广用户统计条数")
    @Parameter(name = "phoneNumber", description = "手机号码")
    @Parameter(name = "activityId", description = "活动id")
    public Rs<Long> getTotal(@RequestParam(required = false) String phoneNumber, @RequestParam(required = false) String activityId) {
        return Rs.success(promotionStatisticsRest.getTotal(phoneNumber, activityId));
    }
}
